/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package api.user;

import Model.User.UserList;
import RssEasy.Core.Encrypt;
import RssEasy.Core.HttpRequestHelper;
import RssEasy.Core.ServletRequestHelper;
import RssEasy.Core.SignVerifyRequestHelper;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "api_user_h5reg", urlPatterns = {"/api/user/h5reg"})
public class h5reg extends ServletRequestHelper {

    @Override
    protected HttpRequestHelper request(HttpServletRequest request, HttpServletResponse response) {
        super.request(request, response);
        try {
            UserList entity = new UserList(this.pageContext);
            UserList bin = new UserList(this.pageContext);
            entity.request();
            if (!this.get("account").matches("^\\S{6,20}")) {
                throw new Exception("100");
            }
            if (!this.get("pwd").matches("^\\S{6,20}")) {
                throw new Exception("101");
            }
            if (!this.get("corpname").matches("^.{2,20}")) {
                throw new Exception("102");
            }
            if (!this.get("linkname").matches("^.{2,5}")) {
                throw new Exception("103");
            }
            if (!this.get("telphone").matches("^1[3578]\\d{9}")) {
                throw new Exception("105");
            }
            if (!this.get("mainbusiness").matches("^.{1,500}")) {
                throw new Exception("104");
            }
            if (!this.get("fulladress").matches("^.{1,50}")) {
                throw new Exception("107");
            }
//            if (bin.select().where("weixin=?", this.get("weixin")).get_first_rows()) {
//                throw new Exception("108");
//            }  
            entity.keyvalue("pwd", Encrypt.Md532(this.get("account") + this.get("pwd")));
            entity.timestamp();
            entity.keyvalue("account", this.get("account"));
            entity.keyvalue("corpname", this.get("corpname"));
            entity.keyvalue("linkname", this.get("linkname"));
            entity.keyvalue("telphone", this.get("telphone"));
            entity.keyvalue("mainbusiness", this.get("mainbusiness"));
            entity.keyvalue("fulladress", this.get("fulladress"));
            if (bin.select("myid").where("corpname=? and fulladress=? ", this.get("corpname"), this.get("fulladress")).get_first_rows()) {
                if (bin.select().where("corpname=? and fulladress=? and account=?", this.get("corpname"), this.get("fulladress"), this.get("account")).get_first_rows()) {
                    throw new Exception("109");
                }
                entity.keyvalue("parentmyid", bin.get("myid"));
                entity.keyvalue("enterprise", bin.get("myid"));
                entity.append().submit();
                out.print("{\"data\":" + bin.get("myid") + "}");
            } else {
                if (bin.select("myid").where("telphone=?", this.get("telphone")).get_first_rows()) {
                    throw new Exception("110");
                }
                entity.keyvalue("parentmyid", 0);
                entity.append().submit();
                out.print("{\"data\":" + entity.autoid + "}");
                entity.keyvalue("enterprise", entity.autoid);
                entity.update().where("myid=" + entity.autoid + "").submit();
            }
        } catch (Exception e) {
            out.print("{\"data\":" + e.getMessage() + "}");
        }
        return this;
    }
}
